package com.smx.xname;
/**
 * @author Grzegorz Bugaj
 * @version 1.2
 * Fixed:Property map was not cleared when new propertytype have been added
 */
import java.util.*;
import java.io.*;
public  class PropertiesContainer {

	private Map<String,String> smxPropMap;
	private Map<String,Map> smxPropContainerMap;
	private boolean preserveAttributeCase;

	public PropertiesContainer ()
    {
    	smxPropContainerMap=new HashMap<String,Map>();
    	smxPropMap=new HashMap<String,String>();
    }
	
	/**
	 * I Return <code>Map<String,Map></code> containing all properties for given property name
	 * @param _propName
	 * @return
	 */
	public Map<String,Map> getPropertyMap(String _propName)
    {
		if(smxPropContainerMap.containsKey(_propName))
		{    		
    		return smxPropContainerMap.get(_propName);
		}
     return null;
    }
	
    public void setProperty(String _propName,String _propAttribute,String _newAttribueValue)
    {
    	_propName=_propName.toLowerCase();
    	_propAttribute=_propAttribute.toLowerCase();
    	
    	if(!isPreserveAttributeCase())
    		_newAttribueValue=_newAttribueValue.toLowerCase();
	
    	Map<String,String> tempMap=new HashMap<String,String>();

    	if(!smxPropContainerMap.containsKey(_propName))
		{
    		smxPropMap=new HashMap<String,String>();
    		smxPropMap.put(_propAttribute,_newAttribueValue);
    		smxPropContainerMap.put(_propName,smxPropMap);
    		
		}else
		{
	
			tempMap=smxPropContainerMap.get(_propName);
			System.out.println(tempMap.size());
			tempMap.put(_propAttribute, _newAttribueValue);
			smxPropContainerMap.remove(_propName);
			smxPropContainerMap.put(_propName,tempMap);			

		}

    }
   
    public void setProperty(String _propList[][])
    {
    	/* format
    	 * _propList[0][0]="LAYOUT";//_propName
    	 * _propList[0][1]="axis";//_propAttribute
    	 * _propList[0][2]="x";//_newAttribue
    	 * 
    	 * 
    	 * _propList[1][0]="LAYOUT";//_propName
    	 * _propList[1][1]="spacing";//_propAttribute
    	 * _propList[1][2]="5";//_newAttribue
    	 */
    	
    	for(int i=0;i<_propList.length;i++)
    	{
    		setProperty(_propList[i][0].toLowerCase(),_propList[i][1].toLowerCase(),_propList[i][2].toLowerCase());
    	}

    }
    
    public boolean isPropertySet(String _propName)
    {
    	_propName=_propName.toLowerCase();
    	return smxPropContainerMap.containsKey(_propName);
    }
    
    public boolean isPropertyAttributeSet(String _propName,String _propAttribute)
    {
    	_propName=_propName.toLowerCase();
    	_propAttribute=_propAttribute.toLowerCase();
    	if(smxPropContainerMap.containsKey(_propName))
    	{
    		Map  tempMap=new HashMap<String,String>();
    		tempMap=smxPropContainerMap.get(_propName);
    		
    		if(tempMap.containsKey(_propAttribute))
    		{
    			 return true;
    		}
    		
    	}
    	return false;
    }
    
    public String readProperty(String _propName,String _propAttribute)
    {
    	_propName=_propName.toLowerCase();
    	_propAttribute=_propAttribute.toLowerCase();
    	if(smxPropContainerMap.containsKey(_propName))
    	{
    		Map  tempMap=new HashMap();
    		tempMap=(Map)smxPropContainerMap.get(_propName);
    		
    		if(tempMap.containsKey(_propAttribute))
    		{
    			 return (String)tempMap.get(_propAttribute);
    		}
    		
    	}
    	return null;  
    } 

    public void removePropertyAttribue(String _propName,String _propAttribute)
    {
    	_propName=_propName.toLowerCase();
    	
    		_propAttribute=_propAttribute.toLowerCase();
    	Map tempMap=new HashMap( );
    	if(smxPropContainerMap.containsKey( _propName ))
    	{
    		tempMap=(Map)smxPropContainerMap.get( _propName );
		
    		if(tempMap.containsKey( _propAttribute ))
    			tempMap.remove( _propAttribute );
		
			smxPropContainerMap.put( _propAttribute, tempMap );
    	}
    } 
 
    public void removeProperty(String _propName)
    {
    	_propName=_propName.toLowerCase();
    	System.out.println("Removing Property:"+_propName);
    	smxPropContainerMap.remove(_propName);

    } 
    
    
	public static void main(String[] args) {
		
		PropertiesContainer temp=new PropertiesContainer();
		temp.setProperty("DBLogin","username","greg");
		System.out.println(temp.readProperty("DBLogin","username"));
	}


	public boolean isPreserveAttributeCase() {
		return preserveAttributeCase;
	}


	public void setPreserveAttributeCase(boolean preserveAttributeCase) {
		this.preserveAttributeCase = preserveAttributeCase;
	}

}
